#!/bin/sh
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

opt_o=false
opt_l=false

opt_error=false

while getopts "ol" option; do
    case $option in
        "o") opt_o=true;;
        "l") opt_l=true;;
        "*") opt_error=true;;
    esac
done

shift $(($OPTIND - 1))
if [ $# -lt 8 ] || [ "$opt_error" = "true" ]; then
    echo "usage: runtests.sh [-o] [-l] <minClients> <maxClients> <deltaClients>"
    echo "         <minCycle> <maxCycle> <deltaCycle> [vespa-fbench options] <hostname> <port>"
    echo ""
    echo "The number of clients varies from <minClients> to <maxClients> with"
    echo "<deltaClients> increments. For each client count, the cycle time will"
    echo "vary in the same way according to <minCycle>, <maxCycle> and <deltaCycle>."
    echo "vespa-fbench is run with each combination of client count and cycle time, and"
    echo "the result output is filtered with the 'vespa-fbench-result-filter.pl' script."
    echo "If you want to save the results you should redirect stdout to a file."
    echo ""
    echo " -o : change the order in which the tests are performed so that client"
    echo "      count varies for each cycle time."
    echo " -l : output a blank line between test subseries. If -o is not specified this"
    echo "      will output a blank line between test series using different client count."
    echo "      If -o was specified this will output blank lines between test series"
    echo "      using different cycle time."
    echo ""
    echo "[vespa-fbench options] <hostname> <port>: These arguments are passed to vespa-fbench."
    echo "  There are 2 things to remenber: first; do not specify either of the -n"
    echo "  or -c options since they will override the values for client count and"
    echo "  cycle time generated by this script. secondly; make sure you specify"
    echo "  the correct host and port number. See the vespa-fbench usage (run vespa-fbench"
    echo "  without parameters) for more info on how to invoke vespa-fbench."
    exit 1
fi

minClients=$1; shift
maxClients=$1; shift
deltaClients=$1; shift
minCycle=$1; shift
maxCycle=$1; shift
deltaCycle=$1; shift

if [ ! $deltaClients -gt 0 ]; then
    echo "error: deltaClients must be greater than 0 !"
    exit 1
fi

if [ ! $deltaCycle -gt 0 ]; then
    echo "error: deltaCycle must be greater than 0 !"
    exit 1
fi

echo "# vespa-fbench results collected by 'runtests.sh'."
echo "#"
echo "#1       2        3     4        5    6    7  8        9   10  11  12  13  14  15  16  17  18      19   20"
echo "#clients duration cycle lowlimit skip fail ok overtime min max avg 25% 50% 75% 90% 95% 99% rate util zerohit"
echo "#--------------------------------------------------------------------------------------------------"

if [ "$opt_o" = "true" ]; then
    cycle=$minCycle
    while [ ! $cycle -gt $maxCycle ]; do
	clients=$minClients
	while [ ! $clients -gt $maxClients ]; do
	    test -f pretest.sh && ./pretest.sh > /dev/null 2>&1
	    vespa-fbench -n $clients -c $cycle $@ | vespa-fbench-result-filter.pl
	    clients=$(($clients + $deltaClients))
	done
	[ "$opt_l" = "true" ] && echo ""
	cycle=$(($cycle + $deltaCycle))
    done
else
    clients=$minClients
    while [ ! $clients -gt $maxClients ]; do
	cycle=$minCycle
	while [ ! $cycle -gt $maxCycle ]; do
	    test -f pretest.sh && ./pretest.sh > /dev/null 2>&1
	    vespa-fbench -n $clients -c $cycle $@ | vespa-fbench-result-filter.pl
	    cycle=$(($cycle + $deltaCycle))
	done
	[ "$opt_l" = "true" ] && echo ""
	clients=$(($clients + $deltaClients))
    done
fi
